import time
from machine import Pin

# 假设28和29是你实际接线的GPIO编号
button1 = Pin(28, Pin.IN, Pin.PULL_UP) # model 1 只检测正方形  io28
button2 = Pin(29, Pin.IN, Pin.PULL_UP) # model 2 只检测圆形    io29
button3 = Pin(20, Pin.IN, Pin.PULL_UP) # model 3 只检测三角形  io20
button4 = Pin(42, Pin.IN, Pin.PULL_UP) # model 4 扩展题       io42
button5 = Pin(43, Pin.IN, Pin.PULL_UP) # model 5 汇总代码      io43

def run_script(filename):
    try:
        with open(filename) as f:
            exec(f.read())
    except Exception as e:
        print("Error running {}: {}".format(filename, e))

# 等待电路稳定
time.sleep(0.5)

while True:
    if button1.value() == 0:  # 按钮1被按下
        #print("Button 1 pressed, running main1.py")
        run_script('/sdcard/main1.py')
        # 等待按钮释放，防止重复触发
        while button1.value() == 0:
            time.sleep(0.05)
    elif button2.value() == 0:  # 按钮2被按下
        #print("Button 2 pressed, running main2.py")
        run_script('/sdcard/main2.py')
        while button2.value() == 0:
            time.sleep(0.05)
    elif button3.value() == 0:  # 按钮3被按下
        #print("Button 3 pressed, running main3.py")
        run_script('/sdcard/main3.py')
        while button3.value() == 0:
            time.sleep(0.05)
    elif button4.value() == 0:  # 按钮4被按下
        #print("Button 4 pressed, running main4.py")
        run_script('/sdcard/main4.py')
        while button4.value() == 0:
            time.sleep(0.05)
    elif button5.value() == 0:  # 按钮5被按下
        #print("Button 5 pressed, running main5.py")
        run_script('/sdcard/main5.py')
        while button5.value() == 0:
            time.sleep(0.05)
    else:
        # 没有按钮被按下，可以选择运行默认脚本或什么都不做
        # print("No button pressed")
        pass
    time.sleep(0.1)  # 主循环延时，减少CPU占用
